package com.example.testproject.activity

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.example.testproject.R
import com.example.testproject.adapter.TopicAdapter
import com.example.testproject.adapter.TopicContentAdapter
import com.example.testproject.bean.TopicFirstBean
import com.example.testproject.bean.TopicSecondBean
import com.example.testproject.utils.click
import kotlinx.android.synthetic.main.recylcer_view.*

/**
 *
 * @ClassName: RvAdvancedSecondActivity
 * @Author: android 超级兵
 * @CreateDate: 10/27/21$ 4:28 PM$
 * TODO recyclerView 进阶2
 */
class RvAdvancedSecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recylcer_view)

        rootRecyclerView.layoutManager = LinearLayoutManager(this)

        val topicAdapter = TopicAdapter(initData())

        rootRecyclerView.adapter = topicAdapter
        topicAdapter.click = object : TopicAdapter.OnTopicListener {
            override fun responseClick(type: Int) {
                countData(topicAdapter)
            }
        }

        countData(topicAdapter)
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 10/28/21 9:55 AM
     * TODO 计算数据
     *  type: 点击回调类型
     */
    private fun countData(adapter: TopicAdapter) {

        val view = layoutInflater.inflate(R.layout.rv_advanced_second_footer, null)
        val textView = view.findViewById<TextView>(R.id.foot_view)
        val btFoot = view.findViewById<TextView>(R.id.btFoot)

        textView.setPadding(10, 10, 10, 10)

        // 单选/多选/判断循环
        adapter.data.filterIsInstance<TopicSecondBean>().map {
            Triple(it.type, it.seq, it.child)
        }.map { pair ->
            pair.third.forEach {
                if (it.isSelect) {
                    textView.append("\t\t${typeJudge(pair.first)} -- 题号为:${pair.second}\t 选中答案为:${it.ContentTitle}\n")
                }
            }
        }

        // 提交点击
        btFoot.click {
            startActivity(Intent(this, TopicSubmitActivity::class.java).also {
                val arrayList = adapter.data as ArrayList<MultiItemEntity>
                it.putExtra(TopicSubmitActivity.DATA_SERIALIZABLE, arrayList)
            })
        }

        adapter.setFooterView(view)
    }


    /*
     * 作者:android 超级兵
     * 创建时间: 10/28/21 10:49 AM
     * TODO 根据type判断类型
     */
    private fun typeJudge(type: Int) = let {
        when (type) {
            TopicAdapter.SINGLE_CHOICE -> {
                "单选题"
            }
            TopicAdapter.MULTIPLE_CHOICE -> {
                "多选题"
            }
            TopicAdapter.JUDGE -> {
                "判断题"
            }
            TopicAdapter.FILL -> {
                "填空题"
            }
            else -> "暂无类型"
        }
    }

    private fun initData() = arrayListOf<MultiItemEntity>(
        TopicFirstBean("单选题"),
        TopicSecondBean(
            TopicAdapter.SINGLE_CHOICE,
            "今天是星期几?",
            1,
            listOf(
                TopicSecondBean.ContentBean(
                    "星期一",
                ),
                TopicSecondBean.ContentBean(
                    "星期三",
                ),
                TopicSecondBean.ContentBean(
                    "星期五",
                ),
                TopicSecondBean.ContentBean(
                    "其他",
                ),
            )
        ),
        TopicSecondBean(
            TopicAdapter.SINGLE_CHOICE,
            "中午吃的什么饭?",
            2,
            listOf(
                TopicSecondBean.ContentBean(
                    "驴肉刀削面",
                ),
                TopicSecondBean.ContentBean(
                    "牛肉刀削面",
                ),
                TopicSecondBean.ContentBean(
                    "猪肉面",
                ),
            )
        ),

        TopicFirstBean("多选题"),
        TopicSecondBean(
            TopicAdapter.MULTIPLE_CHOICE,
            "您爱吃的水果?",
            3,
            listOf(
                TopicSecondBean.ContentBean(
                    "香蕉",
                ),
                TopicSecondBean.ContentBean(
                    "西瓜",
                ),
                TopicSecondBean.ContentBean(
                    "橙子",
                ),
            )
        ),

        TopicFirstBean("判断题"),
        TopicSecondBean(
            TopicAdapter.JUDGE,
            "您的专业是android吗?",
            4,
            listOf(
                TopicSecondBean.ContentBean(
                    "是",
                ),
                TopicSecondBean.ContentBean(
                    "否",
                ),
            )
        ),
        TopicSecondBean(
            TopicAdapter.JUDGE,
            "1 + 1 >= 2 吗?",
            5,
            listOf(
                TopicSecondBean.ContentBean(
                    "是",
                ),
                TopicSecondBean.ContentBean(
                    "否",
                ),
            )
        ),
        TopicFirstBean("填空题"),
        // 填空题
        TopicSecondBean(
            TopicAdapter.FILL,
            "您的生日是__月__日?",
            6,
            listOf(
                TopicSecondBean.ContentBean(
                    "请输入第一个选项",
                    typeLayout = TopicContentAdapter.FILL

                ),
                TopicSecondBean.ContentBean(
                    "请输入第二个选项",
                    typeLayout = TopicContentAdapter.FILL
                ),
            ),
        ),
    )
}